package logic

import (
	"context"
	"product_zc/models/mysql"

	"product_zc/productRpc/internal/svc"
	"product_zc/productRpc/productRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListReplyLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListReplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListReplyLogic {
	return &ListReplyLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListReplyLogic) ListReply(in *productRpc.ListReplyRequest) (*productRpc.ListReplyResponse, error) {
	// todo: add your logic here and delete this line

	var replies []mysql.Reply
	mysql.DB.Debug().Where("product_id = ? ", in.ProductId).Find(&replies)

	var productReplyList []*productRpc.ListReply

	for _, reply := range replies {
		list := productRpc.ListReply{
			Id:        int64(reply.ID),
			Content:   reply.Content,
			UserId:    reply.UserId,
			ProductId: reply.ProductId,
		}

		productReplyList = append(productReplyList, &list)
	}

	return &productRpc.ListReplyResponse{
		List: productReplyList,
	}, nil
}
